package com.ash.test;

import jdk.nashorn.internal.ir.debug.ObjectSizeCalculator;
import org.openjdk.jol.info.ClassLayout;

public class ObjectSize {

    public static void main(String[] args) {
        System.out.println("只打印大小");
        System.out.println(ObjectSizeCalculator.getObjectSize(new Object()));
        System.out.println(ObjectSizeCalculator.getObjectSize(new String()));
        System.out.println(ObjectSizeCalculator.getObjectSize(new int[]{}));
        System.out.println(ObjectSizeCalculator.getObjectSize(new Integer(1)));

        System.out.println("打印对象的内存大小占用情况，详细版本");
        ClassLayout layout = ClassLayout.parseInstance(new Object());
        System.out.println(layout.toPrintable());


        System.out.println();
        ClassLayout layout2 = ClassLayout.parseInstance(new A());
        System.out.println(layout2.toPrintable());

        System.out.println();
        ClassLayout layout1 = ClassLayout.parseInstance(new int[]{});
        System.out.println(layout1.toPrintable());
    }

    public static class A {
        private Integer age;

        private String name;

        private Double balance;

        private Boolean sex;
    }
}
